//
//  TYMBaseVC.m
//  AILock
//
//  Created by 谭亚明on 2020/6/17.
//  Copyright © 2020 tym. All rights reserved.
//

#import "TYMBaseVC.h"
#import "TYMButton.h"
#import "ZFNormalViewController.h"
#import "LoginViewController.h"
#import "LocationVC.h"
#import "OpenLockVC.h"
#import "WellcomViewController.h"
#import "MineViewController.h"
#import "TYMLabel.h"
#import "HomeViewController.h"
#import "UIimage+Extends.h"
#import "Sizes.h"
#import "Fonts.h"
#import "UIColor+HXExtension.h"
#import "AFNetworkReachabilityManager.h"
@interface TYMBaseVC ()<UINavigationControllerDelegate>


@end

@implementation TYMBaseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.delegate = self;
    [self tz_addPopGestureToView:self.view];
    
    self.view.backgroundColor = rgba(245, 245, 245);
    
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
    
    [self monitorReachabilityStatus];
}

//监测网络状态的方法
- (void)monitorReachabilityStatus
{
    // 开始监测
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    // 网络状态改变的回调
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"蜂窝网络");
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@"WIFI");
                break;
            case AFNetworkReachabilityStatusNotReachable:
                [WHToast showMessage:@"您的网络已断开连接" duration:2 finishHandler:nil];
                break;
            case AFNetworkReachabilityStatusUnknown:
                NSLog(@"未知");
                break;
            default:
                break;
        }
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

-(void)rightFunc:(id)sender{
    
}

-(void)addBackBtnFunc{
    [self addLeftBtnImage:@"icon_back"];
}

-(void)addLeftBtnImage:(NSString*)imageName{
    
    TYMButton  *backBtn= [TYMButton btn_Image:imageName];
    [backBtn addTarget:self action:@selector(backViewFunc:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    
    NSArray *arr1=[[NSArray alloc]initWithObjects:barItem, nil];
    self.navigationItem.leftBarButtonItems =arr1;
}

-(void)addRightBtnImage:(NSString*)imageName{
    
    TYMButton  *backBtn= [TYMButton btn_Image:imageName];
    [backBtn addTarget:self action:@selector(rightFunc:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    
    NSArray *arr1=[[NSArray alloc]initWithObjects:barItem, nil];
    self.navigationItem.rightBarButtonItems =arr1;
}

-(void)addTitle:(NSString*)title{
    
    TYMLabel *titleLab = [TYMLabel lb_text:title font:Font_Medium(16) color:[UIColor hx_colorWithHexStr:@"#4A4A4A"]];
    self.navigationItem.titleView = titleLab;
}

-(void)backViewFunc:(id)btn{
    if (self.navigationController) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

-(void)dealloc{
    NSLog(@"++++++++++++++++++释放:%@",[self class]);
    self.navigationController.delegate = nil;
}

#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己MineViewController
    BOOL isShowHomePage = [viewController isKindOfClass:[HomeViewController class]] ||
    [viewController isKindOfClass:[OpenLockVC class]] ||
    [viewController isKindOfClass:[MineViewController class]] ||
    [viewController isKindOfClass:[ZFNormalViewController class]] ||
     [viewController isKindOfClass:[LocationVC class]]||
    [viewController isKindOfClass:[WellcomViewController class]] ||
    [viewController isKindOfClass:[LoginViewController  class]
      ];
    
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}

//收起键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end
